$(document).ready(function(){
    $( "#dashtabs" ).tabs();
    
    // Tipsy Tooltips
    $('.tooltip').tipsy({
        fade: true
    });
    $('.tooltip.north').tipsy({
        fade: true, 
        gravity: 's'
    });
    $('.tooltip.east').tipsy({
        fade: true, 
        gravity: 'w'
    });
    $('.tooltip.west').tipsy({
        fade: true, 
        gravity: 'e'
    });
    // Form Tooltips
    //    $('form [title]').tipsy({
    //        fade: true, 
    //        trigger: 'focus', 
    //        gravity: 'w'
    //    });
    // wysiwyg Toolbar Tooltips
    $('.wysiwyg a').tipsy({
        fade: true, 
        gravity: 's'
    });
    
    //Sidebar Accordion Menu:
		
//    $("#sidebar-nav li ul").hide(); //Hide all sub menus
    
		
    $("#sidebar-nav li a.nav-top-item").click( // When a top menu item is clicked...
        function () {
            $(this).parent().siblings().find("ul").slideUp("normal"); // Slide up all sub menus except the one clicked
            $(this).next().slideToggle("normal"); // Slide down the clicked sub menu
            return false;
        }
        );
		
    $("#sidebar-nav li a.no-submenu").click( // When a menu item with no sub menu is clicked...
        function () {
            window.location.href=(this.href); // Just open the link instead of a sub menu
            return false;
        }
        ); 

    // Sidebar Accordion Menu Hover Effect:
		
    $("#sidebar-nav li .nav-top-item").hover(
        function () {
            $(this).stop();
            $(this).animate({
                paddingLeft: "20px"
            }, 200);
        }, 
        function () {
            $(this).stop();
            $(this).animate({
                paddingLeft: "5px"
            });
        });
            
    
});

//remember click toogle sidebar
$('#sidebar-nav ul.drop li a').click(
    function(){
        var id = $(this).parent().parent().parent().attr("id");
        $.cookie('sidebarnav',id);
    }
);
       
function show_nav(){
    $("#sidebar-nav li ul").hide();
    $('#sidebar-nav #' + sidebarnav + ' ul').slideToggle("slow"); // Slide down the current menu item's sub menu
}
var sidebarnav = $.cookie('sidebarnav');
if(sidebarnav != "underfined"){
    show_nav();
}

// Sidebar Toggle
var fluid = {
    Toggle : function(){
        var default_hide = {
            "grid": true
        };
        $.each(
            ["pagesnav", "commentsnav", "userssnav", "imagesnav"],
            function() {
                var el = $("#" + (this == 'accordon' ? 'accordion-block' : this) );
                if (default_hide[this]) {
                    el.hide();
                    $("[id='toggle-"+this+"']").addClass("hidden")
                }
                $("[id='toggle-"+this+"']")
                .bind("click", function(e) {
                    if ($(this).hasClass('hidden')){
                        $(this).removeClass('hidden').addClass('visible');
                        el.slideDown();
                    } else {
                        $(this).removeClass('visible').addClass('hidden');
                        el.slideUp();
                    }
                    e.preventDefault();
                });
            }
            );
    }
}
jQuery(function ($) {
    if($("[id^='toggle']").length){
        fluid.Toggle();
    }
});


// Notification Animations
$(function () { 
    $('.notification').hide().append('<span class="close" title="Dismiss"></span>').fadeIn('slow');
    $('.notification .close').hover(
        function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        }
        );
    $('.notification .close').click(function() {
        $(this).parent().fadeOut('slow', function() {
            $(this).remove();
        });
    }); 

});

function clearText(field)
{
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

// jQuery UI - Live Search
$(function() {
    var availableTags = ["dashboard", "pages", "manage pages", "edit pages", "delete pages", "users", "manage users", "edit users", "delete users", "settings", "system settings", "server settings", "documentation", "help", "community forums", "contact"];
    $("#livesearch").autocomplete({
        source: availableTags
    });
});



// jQuery UI - Dialog Box
$(function() {
    $('#dialog').dialog({
        autoOpen: false,
        modal: true,
        width: 500
    })
    $('#opener').click(function() {
        $('#dialog').dialog('open');
        return false;
    });

});
	
	



// Sidebar close/open (with cookies)
function close_sidebar() {
    $("body").addClass('sidebar-off');
    $("#open_sidebar").show();
    $("#close_sidebar").hide();
    $.cookie('sidebar', 'closed');
    if( $('body').hasClass('chart') ) {
        // redraw chart
        drawChart();
    }
}
function open_sidebar() {
    $("body").removeClass('sidebar-off');
    $("#open_sidebar").hide();
    $("#close_sidebar").show();
    $.cookie('sidebar', 'open');
    if( $('body').hasClass('chart') ) {
        // redraw chart
        drawChart();
    }
}
$('#close_sidebar').click(function(){
    close_sidebar();
/*if($.browser.safari) {
 location.reload();
 }*/
});
$('#open_sidebar').click(function(){
    open_sidebar();
/*if($.browser.safari) {
 location.reload();
 }*/
});
var sidebar = $.cookie('sidebar');

if (sidebar == 'closed') {
    close_sidebar();
};

if (sidebar == 'open') {
    open_sidebar();
};
 
 
// Filetree Setup
$(".filetree").treeview({
    control: "#treecontrol"
});
$("#filetree-add").click(function() {
    var branches = $("<li><span class='folder'>New Sublist</span><ul>" + 
        "<li><img alt='' src='./img/icons/16/page.png' /><a href='#'>New Item</a></li>" + 
        "<li><img alt='' src='./img/icons/16/page.png' /><a href='#'>New Item</a></li></ul></li>").appendTo("#browser");
    $("#browser").treeview({
        add: branches
    });
    branches = $("<li class='closed'><span class='folder'>New Sublist</span><ul><li><img alt='' src='./img/icons/16/page.png' /><a href='#'>New Item</a></li><li><img alt='' src='./img/icons/16/page.png' /><a href='#'>New Item</a></li></ul></li>").prependTo("#folder21");
    $("#browser").treeview({
        add: branches
    });
});
	
	
// wysiwyg Setup
$('.wysiwyg').wysiwyg({
    controls: {
        strikeThrough : {
            visible : false
        },
        underline     : {
            visible : false
        },
      
        separator00 : {
            visible : false
        },
      
        justifyLeft   : {
            visible : false
        },
        justifyCenter : {
            visible : false
        },
        justifyRight  : {
            visible : false
        },
        justifyFull   : {
            visible : false
        },
      
        separator01 : {
            visible : false
        },
      
        indent  : {
            visible : false
        },
        outdent : {
            visible : false
        },
      
        separator02 : {
            visible : false
        },
      
        subscript   : {
            visible : false
        },
        superscript : {
            visible : false
        },
      
        separator03 : {
            visible : false
        },
      
        undo : {
            visible : false
        },
        redo : {
            visible : false
        },
      
        separator04 : {
            visible : false
        },
      
        insertOrderedList    : {
            visible : false
        },
        insertUnorderedList  : {
            visible : false
        },
        insertHorizontalRule : {
            visible : false
        },
      
        separator07 : {
            visible : false
        },
      
        cut   : {
            visible : false
        },
        copy  : {
            visible : false
        },
        paste : {
            visible : false
        }	
    }
});

// Make objects collapsible
$(".collapsible-list span.toggle").click(function(){
    $(this).closest('li').toggleClass('collapsed');
    return false;
});
$("fieldset legend > a, .fieldset .legend > a").click(function(){
    $(this).closest('fieldset, .fieldset').toggleClass('collapsed');
    $("fieldset legend a").toggleClass('collapse');
    return false;
});			




// Form Switches
$(".switch-enable").click(function(){
    var parent = $(this).parents('.switch-wrapper');
    $('.switch-disable',parent).removeClass('selected');
    $(this).addClass('selected');
    $('.checkbox',parent).attr('checked', true);
});
$(".switch-disable").click(function(){
    var parent = $(this).parents('.switch-wrapper');
    $('.switch-enable',parent).removeClass('selected');
    $(this).addClass('selected');
    $('.checkbox',parent).attr('checked', false);
});


// Check all checkboxes when the one in a table head is checked:

//$(function () { // this line makes sure this code runs on page load
//    $('.check-all').click(function () {
//        $(this).parents('table:eq(0)').find(':checkbox').attr('checked', this.checked);
//    });
//});
 
// Callback that creates and populates a data table, 
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {

    // Create our data table.
    var countries = ['Unique Visits', 'Page Views'];
    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
    var productionByCountry = [[165, 135, 157, 139, 136, 165],
    [186, 164, 172, 165, 155, 180]];

    // Create and populate the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Month');
               
    for (i = 0; i < countries.length; i++) {
        data.addColumn('number', countries[i]);
    }
    data.addRows(months.length);
    for (i = 0; i < months.length; ++i) {
        data.setCell(i, 0, months[i]);
    }
    for (i = 0; i < countries.length; ++i) {
        var country = productionByCountry[i];
        for (var month = 0; month < months.length; ++month) {
            data.setCell(month, i + 1, country[month]);
        }
    }

    // Instantiate and draw our chart, passing in some options.
//    var div = $('#chart_div');
//    var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
//    chart.draw(data, {
//        width: div.width(), 
//        height: 350, 
//        is3D: true, 
//        title: '6 Month Traffic Report', 
//        hAxis: {
//            title: "Month"
//        }, 
//        vAxis: {
//            title: "Views"
//        }
//    });
}